package com.xinyun.traceability.controller;

import com.wf.captcha.ArithmeticCaptcha;
import com.xinyun.traceability.enity.po.SysUser;
import com.xinyun.traceability.service.SysUserService;
import com.xinyun.traceability.utils.R;
import com.xinyun.traceability.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登陆控制器
 */
@Controller
public class LoginController {

    @Autowired
    SysUserService sysUserService;

    /**
     * 进入登录页
     */
    @GetMapping("/login")
    public String login(HttpSession session) {
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
        if (sysUser != null) {
            return "redirect:main";
        }
        return "login";
    }

    /**
     * 注册页
     */
    @GetMapping("/reg")
    public String reg() {
        return "reg";
    }

    /**
     * 生成验证码 算术类型
     */
    @RequestMapping("/assets/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        captcha.setLen(2);
        captcha.getArithmeticString();
        captcha.text();
        request.getSession().setAttribute("captcha", captcha.text());
        captcha.out(response.getOutputStream());
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    @ResponseBody
    public R login(HttpServletRequest request, String username, String password, String code, boolean rememberMe) {
        if (StringUtil.isBlank(username, password)) {
            return R.failed("账号或密码不能为空");
        }
        String sessionCode = (String) request.getSession().getAttribute("captcha");
        if (code == null || !sessionCode.equals(code.trim().toLowerCase())) {
            return R.failed("验证码不正确");
        }
        //根据用户名密码查询用户
        SysUser sysUser = sysUserService.selectByUsernameAndPassword(username, password);
        if (sysUser == null) {
            return R.failed("用户名或密码错误");
        }
        request.getSession().setAttribute("sysUser", sysUser);
        return R.succeed("登录成功");
    }

    /**
     * 保存用户注册
     */
    @PostMapping("/reg")
    @ResponseBody
    public R register(SysUser user, HttpServletRequest request, String code) {
        String sessionCode = (String) request.getSession().getAttribute("captcha");
        if (code == null || !sessionCode.equals(code.trim().toLowerCase())) {
            return R.failed("验证码不正确");
        }
        //验证用户名是否已注册过
        SysUser sysUser = sysUserService.selectByUsernameAndPassword(user.getUsername(), null);
        if (sysUser != null) {
            return R.failed("用户名已存在");
        }
        try {
            sysUserService.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("注册失败");
        }
        return R.succeed("注册成功");
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:login";
    }

}
